<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Contact extends Common
{
    /**
     * 咨询用户
     * */
    public function userChat()
    {
        // 读取未结束消息
        $uid = session('id');

        // 待连线用户
        $wait_user_list = Db::name('chat c')
            ->join('tp_user u', 'c.from_uid = u.id', 'left')
            ->field('u.id,u.avatar,u.username')
            ->where(['c.to_uid' => 0, 'c.is_over' => 0])
            ->group('c.from_uid')
            ->order('c.create_time asc')
            ->select();

        if ($uid) {
            $user_list = Db::name('chat c')
                ->join('tp_user u', 'c.from_uid = u.id', 'left')
                ->field('u.id,u.avatar,u.username')
                ->where(['c.to_uid' => $uid, 'c.is_over' => 0])
                ->group('c.from_uid')
                ->order('c.is_read asc, c.create_time desc')
                ->select();

            if ($user_list) {
                foreach ($user_list as &$v) {
                    $v['avatar'] = doAvatar($v['avatar']);
                    $user_id = $v['id'];

                    $new_msg = Db::name('chat')
                        ->where(['to_uid' => $uid, 'from_uid' => $user_id, 'is_over' => 0])
                        ->whereOr(
                            function ($query) use ($user_id, $uid) {
                                $query->where(['from_uid' => $uid, 'to_uid' => $user_id, 'is_over' => 0]);
                            }
                        )
                        ->order('create_time desc')
                        ->value('msg');
                    $v['msg'] = $new_msg;
                }

                $user_list_id = $user_list[0]['id'];

                $msg_list = $this->getMsgList($uid, $user_list_id);

                Db::name('chat')->where(['to_uid' => $uid, 'from_uid' => $user_list[0]['id'], 'is_read' => 0])->setField('is_read', 1);
            }
        }
        $avatar = Db::name('user')->where(['id' => $uid])->value('avatar');

        $this->assign('wait_user_list', $wait_user_list);
        $this->assign('user_list', $user_list ? : []);
        $this->assign('msg_list', $msg_list ? : []);
        $this->assign('avatar', doAvatar($avatar));
        $this->assign('show_user_list', json_encode(array_column($user_list, 'id')));

        $this->assign('title', '咨询用户');

        return $this->view->fetch();
    }

    /**
     * 接入用户
     * */
    public function linkUser()
    {
        $input = Request()->param();

        $user_id = $input['user_id'];
        $contact_id = $input['contact_id'];

        if (empty($user_id)) apiReturn(2, '用户ID缺失');
        if (empty($contact_id)) apiReturn(2, '客服ID缺失');

        $res = Db::name('chat')->where(['from_uid' => $user_id, 'to_uid' => 0])->setField('to_uid', $contact_id);
        if ($res) {
            apiReturn(1, '接入成功');
        } else {
            apiReturn(2, '接入失败');
        }

    }

    /**
     * 获取用户聊天内容
     * */
    public function getUserChatContent()
    {
        $input = Request()->param();

        $user_id = $input['user_id'];

        if (empty($user_id)) apiReturn(2, '用户ID缺失');

        $uid = session('id');

        $msgList = $this->getMsgList($uid, $user_id);

        $username = Db::name('user')->where(['id' => $user_id])->value('username');

        $data = [
            'username' => $username,
            'msgList' => $msgList
        ];

        apiReturn(1, '获取数据成功', $data);
    }

    /**
     * 获取某一个用户的聊天消息内容
     * @param int $contact_id 客服id
     * @param int $user_id    用户id
     * @return array
     * */
    public function getMsgList($contact_id, $user_id)
    {
        $msg_list = Db::name('chat c')
            ->field('c.from_uid,from.avatar from_avatar,c.msg,c.create_time')
            ->join('tp_user from', 'c.from_uid = from.id')
            ->whereOr(
                function ($query) use ($contact_id, $user_id) {
                    $query->where(['c.to_uid' => $contact_id, 'c.from_uid' => $user_id, 'c.is_over' => 0]);
                }
            )
            ->whereOr(
                function ($query) use ($contact_id, $user_id) {
                    $query->where(['c.to_uid' => $user_id, 'c.from_uid' => $contact_id, 'c.is_over' => 0]);
                }
            )
            ->order('c.create_time asc')
            ->select();

        if ($msg_list) {
            foreach ($msg_list as &$v) {
                $v['from_avatar'] = doAvatar($v['from_avatar']);
                $v['create_time'] = date('H:i:s', $v['create_time']);
            }
            return $msg_list;
        } else {
            return [];
        }
    }
}